package com.itheima.util;

import java.util.Random;

public class CodeUtil2 {
    public CodeUtil2() {

    }

    static char[] ziMu = new char[52 + 10];

    static {
        //存入字母
        for (int i = 0; i < ziMu.length; i++) {
            if (i < 26) {
                ziMu[i] = (char) (i + 'a');
            } else if (i >= 26 && i < 52) {
                ziMu[i] = (char) (i - 26 + 'A');
            } else {
                ziMu[i] = (char) (i - 52 + '0');
            }
        }
    }

    public static String getCode() {
        Random r = new Random();
        StringBuilder sb = new StringBuilder();


        //随机4个字母的索引并添加到sb
        int i1 = r.nextInt(2) + 4;
        for (int i = 0; i < i1; i++) {
            int index = r.nextInt(52 + 10);
            //如果是数字就从字符转为数字再添加到sb
            if (index >= 52) {
                sb.append((int) ziMu[index]);
            } else {
                sb.append(ziMu[index]);
            }
        }

        String str = sb.toString();
        //判断两个数字是否连在一起

        return str;
    }

}
